From: Ian Jackson Date: Tue, 7 Apr 2015 13:05:28 +0000 (+0100) Subject: libxl: Cope with pipes which signal POLLHUP|POLLIN on read eof X-Git-Tag: archive/raspbian/4.8.0-1+rpi1~1^2~3428 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/%22/%22http:/www.example.com/cgi/%22?a=commitdiff_plain;h=da7e6b3108a72941e7bf35f48495c67975a257e4;p=xen.git libxl: Cope with pipes which signal POLLHUP|POLLIN on read eof Some operating systems (including Linux and FreeBSD[1]) signal not (only) POLLIN when a reading pipe reaches EOF, but POLLHUP (with or without POLLIN). This is permitted[2]. The implications are that in the general case it is not possible to determine whether POLLHUP indicates an error or simply eof without attempting a read. Datacopiers mishandle this, because they always treat POLLHUP exceptionally (either reporting it via callback_pollhup, or treating it as an error). datacopiers reading from pipes on such OSs can fail (perhaps leaving some data unprocessed) rather than completing successfully. [1] http://www.greenend.org.uk/rjk/tech/poll.html [2] http://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html Distinguishing POLLHUP is needed for pty fds, but most callers in libxl do not care about POLLHUP except as an error or eof condition. So change the datacopier semantics so that if callback_pollhup is not specified we treat POLLHUP almost like POLLIN. The difference is that if we get HUP from poll, but EWOULDBLOCK from read, we must signal an error rather than attempting the read again. This fixes the problem which 7e9ec50b0535 was aimed at. Signed-off-by: Ian Jackson CC: Ian Campbell CC: Andrew Cooper CC: Roger Pau Monné CC: Ross Lagerwall CC: Wei Liu Acked-by: Wei Liu Acked-by: Ian Campbell --- diff --git a/tools/libxl/libxl_aoutils.c b/tools/libxl/libxl_aoutils.c index ddbe6aebd6..ef679dd553 100644 --- a/tools/libxl/libxl_aoutils.c +++ b/tools/libxl/libxl_aoutils.c @@ -208,13 +208,14 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev, if (datacopier_pollhup_handled(egc, dc, revents, 0)) return; - if (revents & ~POLLIN) { - LOG(ERROR, "unexpected poll event 0x%x (should be POLLIN)" + if (revents & ~(POLLIN|POLLHUP)) { + LOG(ERROR, + "unexpected poll event 0x%x (expected POLLIN and/or POLLHUP)" " on %s during copy of %s", revents, dc->readwhat, dc->copywhat); datacopier_callback(egc, dc, -1, 0); return; } - assert(revents & POLLIN); + assert(revents & (POLLIN|POLLHUP)); for (;;) { libxl__datacopier_buf *buf = NULL; int r; @@ -243,7 +244,17 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev, } if (r < 0) { if (errno == EINTR) continue; - if (errno == EWOULDBLOCK) break; + if (errno == EWOULDBLOCK) { + if (revents & POLLHUP) { + LOG(ERROR, + "poll reported HUP but fd read gave EWOULDBLOCK" + " on %s during copy of %s", + dc->readwhat, dc->copywhat); + datacopier_callback(egc, dc, -1, 0); + return; + } + break; + } LOGE(ERROR, "error reading %s during copy of %s", dc->readwhat, dc->copywhat); datacopier_callback(egc, dc, 0, errno); diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 9c2230939f..10d12526ac 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -2539,7 +2539,8 @@ typedef struct libxl__datacopier_buf libxl__datacopier_buf; * errnoval!=0 means we had a read error, logged * onwrite==-1 means some other internal failure, errnoval not valid, logged * If we get POLLHUP, we call callback_pollhup(..., onwrite, -1); - * or if callback_pollhup==0 this is an internal failure, as above. + * or if callback_pollhup==0 this is treated as eof (if POLLIN|POLLHUP + * on the reading fd) or an internal failure (otherwise), as above. * In all cases copier is killed before calling this callback */ typedef void libxl__datacopier_callback(libxl__egc *egc, libxl__datacopier_state *dc, int onwrite, int errnoval);